package com.simple.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simple.common.entity.po.TradeEntity;
import com.simple.ds.mapper.TradeMapper;
import com.simple.ds.service.TradeService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yasud
 * @date 2021年09月17日 22:43
 * @description
 */
@Service
public class TradeServiceImpl extends ServiceImpl<TradeMapper, TradeEntity> implements TradeService {

    @Override
    public List<TradeEntity> findByParam(Map<String, Object> param) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper();
        if (param.containsKey("merchant_order_no")) {
            queryWrapper.lambda().eq(TradeEntity::getMerchantOrderNo,  param.get("merchant_order_no"));
        }
        if (param.containsKey("app_id")) {
            queryWrapper.lambda().eq(TradeEntity::getMerchantCode, param.get("app_id"));
        }
        else {
            queryWrapper.eq("1", "2");
        }
        List<TradeEntity> tradeEntity = list(queryWrapper);
        return tradeEntity;
    }

    @Override
    public TradeEntity findOneByParam(Map<String, Object> param) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper();
        if (param.containsKey("merchant_order_no")) {
            queryWrapper.lambda().eq(TradeEntity::getMerchantOrderNo,  param.get("merchant_order_no"));
        }
        if (param.containsKey("app_id")) {
            queryWrapper.lambda().eq(TradeEntity::getMerchantCode, param.get("app_id"));
        }
        else {
            queryWrapper.eq("1", "2");
        }
        TradeEntity tradeEntity = getOne(queryWrapper);
        return tradeEntity;
    }


    @Override
    public int findByRiskDoNotHour(String cardNo, String cardHolder, String channelStatus, Date date, Long channelId) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TradeEntity> lambda = queryWrapper.lambda();
        lambda
                .eq(TradeEntity::getChannelMerchantId, channelId)
                .eq(TradeEntity::getChannelStatus, channelStatus)
                .ge(TradeEntity::getCreateTime, date)
                .and(tradeLambdaQueryWrapper -> {
                    LambdaQueryWrapper<TradeEntity> like = tradeLambdaQueryWrapper.like(TradeEntity::getRemark1, "%" + cardHolder + "%");
                    like.or().like(TradeEntity::getRemark1, "%" + cardNo + "%");
                });
        int count = count(queryWrapper);
        return count;
    }

    @Override
    public TradeEntity findByPropertyOne_(String k, Object v) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(k, v);
        return getOne(queryWrapper);
    }

    @Override
    public int findByRiskConfigError(String cardNo, String channelStatus, Date date, Long channelId) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TradeEntity> lambda = queryWrapper.lambda();
        lambda
                .eq(TradeEntity::getChannelMerchantId, channelId)
                .eq(TradeEntity::getRemark1, "%" + cardNo + "%")
                .eq(TradeEntity::getChannelStatus, channelStatus)
                .ge(TradeEntity::getCreateTime, date);
        int count = count(queryWrapper);
        return count;
    }

    @Override
    public int findByRiskInvalidTransaction(String cardHolder, String channelStatus, Date date, Long channelId) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TradeEntity> lambda = queryWrapper.lambda();
        lambda
                .eq(TradeEntity::getChannelMerchantId, channelId)
                .eq(TradeEntity::getChannelStatus, channelStatus)
                .like(TradeEntity::getRemark1, "%" + cardHolder + "%")
                .ge(TradeEntity::getCreateTime, date);
        int count = count(queryWrapper);
        return count;
    }

    @Override
    public int findByRiskRestrictedCard(String channelStatus, String cardNo, String cardHolder, Date date, Long channelId) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TradeEntity> lambda = queryWrapper.lambda();
        lambda
                .eq(TradeEntity::getChannelMerchantId, channelId)
                .eq(TradeEntity::getChannelStatus, channelStatus)
                .ge(TradeEntity::getCreateTime, date)
                .and(tradeLambdaQueryWrapper -> {
                    LambdaQueryWrapper<TradeEntity> like = tradeLambdaQueryWrapper.like(TradeEntity::getRemark1, "%" + cardHolder + "%");
                    like.or().like(TradeEntity::getRemark1, "%" + cardNo + "%");
                });
        int count = count(queryWrapper);
        return count;
    }

    @Override
    public int findByRiskCardHolderCount(String ipAddress, String cardNo, String cardHolder, String date, Long channelId) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TradeEntity::getChannelMerchantId, channelId)
                .apply("date_format(create_time,'%Y-%m-%d') = {0}", date)
                .and(tradeLambdaQueryWrapper -> {
                    tradeLambdaQueryWrapper.like(TradeEntity::getRemark1, "%" + cardHolder + "%"+cardNo+"%");
                })
                .and(tradeLambdaQueryWrapper -> {
                    tradeLambdaQueryWrapper.like(TradeEntity::getExtraInfo, "%" + ipAddress + "%");
                });
        int count = count(queryWrapper);
        return count;
    }

    @Override
    public List<TradeEntity> findTradeByChannelOrderNo(String channelTradeNo) {
        QueryWrapper<TradeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TradeEntity::getChannelOrderNo, channelTradeNo);
        return list(queryWrapper);
    }
}
